using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;

namespace JNPF.Basics.Logic
{
    public class CustomervehicleService : DataFactory, CustomervehicleIService
    {
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<CustomervehicleEntity> GetInfo(int id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<CustomervehicleEntity>(m => m.Id == id);
            }
        }

        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomervehicleEntity>> GetPageList(CustomervehicleListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<CustomervehicleEntity>();
                var queryParam = input.queryJson.ToObject();
                //查询客户主键
                //查询客户主键
                if (input.customerId.HasValue)
                {
                    queryWhere = queryWhere.And(m => m.CustomerId == input.customerId);
                }
                //查询品牌型号
                if (!string.IsNullOrEmpty(input.brandModel))
                {
                    queryWhere = queryWhere.And(m => m.BrandModel.Contains(input.brandModel));
                }
                ////查询购买年份
                //if (!string.IsNullOrEmpty(input.purchaseYear))
                //{
                //	queryWhere = queryWhere.And(m => m.PurchaseYear.Contains(input.purchaseYear));
                //}
                //查询购买原值
                if (input.purchaseOriginalValue.ToDecimal() > 0)
                {
                    queryWhere = queryWhere.And(m => m.PurchaseOriginalValue == input.purchaseOriginalValue);
                }
                //查询账面净值
                if (input.bookNetValue.ToDecimal() > 0)
                {
                    queryWhere = queryWhere.And(m => m.BookNetValue == input.bookNetValue);
                }
                ////查询是否抵押
                //if (!string.IsNullOrEmpty(input.isMortgaged))
                //{
                //	queryWhere = queryWhere.And(m => m.IsMortgaged.Contains(input.isMortgaged));
                //}

                //查询客户ID
                if (input.customerId.HasValue)
                {
                    queryWhere = queryWhere.And(m => m.CustomerId == input.customerId);
                }
                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<CustomervehicleEntity>().Where(queryWhere).GetPageResultAsync(input);
            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(CustomervehicleEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(CustomervehicleEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(CustomervehicleEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<List<CustomervehicleEntity>> GetList(CustomervehicleListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<CustomervehicleEntity>();
                //查询客户主键
                if (input.customerId.HasValue)
                {
                    queryWhere = queryWhere.And(m => m.CustomerId == input.customerId);
                }
                //查询品牌型号
                if (!string.IsNullOrEmpty(input.brandModel))
                {
                    queryWhere = queryWhere.And(m => m.BrandModel.Contains(input.brandModel));
                }
                ////查询购买年份
                //if (!string.IsNullOrEmpty(input.purchaseYear))
                //{
                //	queryWhere = queryWhere.And(m => m.PurchaseYear.Contains(input.purchaseYear));
                //}
                //查询购买原值
                if (input.purchaseOriginalValue.ToDecimal() > 0)
                {
                    queryWhere = queryWhere.And(m => m.PurchaseOriginalValue == input.purchaseOriginalValue);
                }
                //查询账面净值
                if (input.bookNetValue.ToDecimal() > 0)
                {
                    queryWhere = queryWhere.And(m => m.BookNetValue == input.bookNetValue);
                }
                ////查询是否抵押
                //if (!string.IsNullOrEmpty(input.isMortgaged))
                //{
                //	queryWhere = queryWhere.And(m => m.IsMortgaged.Contains(input.isMortgaged));
                //}


                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<CustomervehicleEntity>().Where(queryWhere).ToListAsync();
            }
        }


    }
}